#define BLINKER_WIFI
#include <Arduino.h>
#include <U8g2lib.h>
#include <Blinker.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <SPI.h>
#include <DHT.h>

//调用GPIO
#define D0  16//LED_R
#define D1  05//LED_G
#define D2  04//LED_B
#define D3  00//DHT11_PIN
#define D4  02//
#define D5  14//u8g2_Clock
#define D6  12//u8g2_Data
#define D7  13
#define D8  15
#define D9  03
#define D10 01
#define S2  09
#define S3  10

//数据定义
//int shock_val = 0;
int i=0,j=0,k=0;//RGB数值

//DHT类型为DHT11
#define DHTTYPE DHT11

//u8g2定义
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ D5, /* data=*/ D6, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display

//按钮定义
BlinkerButton Button1("btn-abc");//按钮1btn-abc
BlinkerSlider Slider_R("Slider_R");//红灯滑块
BlinkerSlider Slider_G("Slider_G");//绿灯滑块
BlinkerSlider Slider_B("Slider_B");//蓝灯滑块
//BlinkerNumber Number1("num-abc");//计数按钮  
BlinkerButton U8g2("U8g2firstpage");//按键翻页
BlinkerNumber HUMI("humi");//温度数据
BlinkerNumber TEMP("temp");//湿度数据
/*
BlinkerButton Button_R("btn-86a");//红灯按钮
BlinkerButton Button_G("btn-r3m");//绿灯按钮
BlinkerButton Button_B("btn-gdl");//蓝灯按钮
*/

char auth[] ="825bfd4e2928";//秘钥
char ssid[] ="Gao";//WIFI名称
char pswd[] ="gao51920";//WIFI密码

//DHT11初始化
DHT dht(D3, DHTTYPE);
uint32_t read_time = 0;
float humi_read, temp_read;

//int counter = 0;

//点亮板载LED
void Button1_callback(const String& state){
    BLINKER_LOG("get Button1 state: ", state);
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}

//读取数据
void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);
    Blinker.vibrate();
    uint32_t BlinkerTime = millis();
    //Blinker.print(BlinkerTime);
    Blinker.print("millis", BlinkerTime);
}

// 温湿度数据发送
void heartbeat()
{
    HUMI.print(humi_read);
    TEMP.print(temp_read);
}

void Slider_R_callback(int32_t value)
{
    BLINKER_LOG("get slider value: ", value);
    i=value;
}

void Slider_G_callback(int32_t value)
{
    BLINKER_LOG("get slider value: ", value);
    j=value;
}
void Slider_B_callback(int32_t value)
{
    BLINKER_LOG("get slider value: ", value);
    k=value;
}

/*
 * 计数函数数据读取
void dataRead(const String& data){
    BLINKER_LOG("Blinker readString: ", data);
    counter++;
    Number1.print(counter);
}
*/

/*
//红灯回调函数
void R_callback(const String &state)
{  
   digitalWrite(D0,!digitalRead(D0));
   BLINKER_LOG("R_val:",state);
}

//绿灯回调函数
void G_callback(const String &state)
{  
   digitalWrite(D1,!digitalRead(D1));
   BLINKER_LOG("G_val:",state);
}

//蓝灯回调函数
void B_callback(const String &state)
{  
   digitalWrite(D2,!digitalRead(D2));
   BLINKER_LOG("B_val:",state);
}
*/

//U8g2按键翻页
void U8g2_F(const String &state)
{
    BLINKER_LOG("get U8g2 state: ", state);//获取按键状态
    u8g2.clearDisplay();
    u8g2.setFont(u8g2_font_unifont_t_chinese2);  // use chinese2 for all the glyphs of "你好世界"
    u8g2.setFontDirection(0);
    u8g2.firstPage();
    do{        
        //输出字符串
        u8g2.setCursor(0, 15);
        u8g2.print("高晓龙");
        //u8g2.setFont(u8g2_font_ncenB10_tr);//设置字体：10号
        //u8g2.drawStr(0,11,"Hello World!");
        //u8g2.drawStr(0,22,"Gao Xiaolong");
    }while( u8g2.nextPage() );
    delay(5000);//只显示5000ms
}

//U8g2显示
void u8g2run(void)
{     
    u8g2.setFont(u8g2_font_unifont_t_chinese2);  // use chinese2 for all the glyphs of "你好世界"
    u8g2.setFontDirection(0);
    u8g2.firstPage();
    do{
        //输出字符串
        u8g2.setCursor(0, 15);
        u8g2.print("美好的一天");
        //u8g2.setFont(u8g2_font_ncenB10_tr);//设置字体：10号  
        //u8g2.drawStr(0,11,"Hello World!");
        //u8g2.drawStr(0,22,"Gao Xiaolong");
    }while( u8g2.nextPage() );
}

//DHT11温湿度获取
void DHT11run()
{
    if (read_time == 0 || (millis() - read_time) >= 2000)
    {
        read_time = millis();
        float h = dht.readHumidity();
        float t = dht.readTemperature();
        if (isnan(h) || isnan(t)) {
            BLINKER_LOG("Failed to read from DHT sensor!");
            return;
        }
        float hic = dht.computeHeatIndex(t, h, false);
        humi_read = h;
        temp_read = t;
        BLINKER_LOG("Humidity: ", h, " %");
        BLINKER_LOG("Temperature: ", t, " *C");
        BLINKER_LOG("Heat index: ", hic, " *C");
    } 
}

void RGBrun()
{
      analogWrite(D0,i);//Red
      analogWrite(D1,j);//Green
      analogWrite(D2,k);//Blue
}

//敲击传感器
/*
void shockrun()
{
      int i;
      shock_val=digitalRead(D4);
      if(shock_val==HIGH)
      { 
          digitalWrite(LED_BUILTIN,LOW);
          //delay(1000);
      }
      else
      {
          digitalWrite(LED_BUILTIN,HIGH);
          //delay(1000);
      }
}
*/

void setup(){
    Serial.begin(9600);
    BLINKER_DEBUG.stream(Serial);
    BLINKER_DEBUG.debugAll();

    // 初始化RGB灯
    pinMode(D0,OUTPUT);
    pinMode(D1,OUTPUT);
    pinMode(D2,OUTPUT);
    digitalWrite(D0,LOW);
    digitalWrite(D1,LOW);
    digitalWrite(D2,LOW);

    //定义Shock_PIN为输入引脚
    //pinMode(D4,INPUT);

    //初始化板载LED
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);
    
    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);
    Blinker.attachHeartbeat(heartbeat);
    
    //回调函数注册
    Slider_R.attach(Slider_R_callback);
    Slider_G.attach(Slider_G_callback);
    Slider_B.attach(Slider_B_callback);

    //U8g2翻页注册
    U8g2.attach(U8g2_F);
    //允许输出UTF8    
    u8g2.enableUTF8Print();    
    //板载LED按键注册
    Button1.attach(Button1_callback);
    u8g2.begin();
    dht.begin();
}

void loop(){    
    Blinker.run();//Blinker运行
    u8g2run();//u8g2运行
    DHT11run();//DHT11运行
    RGBrun();
    //shockrun();//敲击传感器运行
}
